﻿using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using PhotoFramePublishingPlugin.Properties;

namespace PhotoFramePublishingPlugin
{
    internal class ConfigurationSettingsPresenter : INotifyPropertyChanged
    {
        private readonly IConfigurationSettingsView _view;
        private readonly IConfigurationSettingsModel _model;

        public ConfigurationSettingsPresenter(IConfigurationSettingsView view, IConfigurationSettingsModel model)
        {
            if (view == null)
                throw new ArgumentNullException("view");
            if (model == null)
                throw new ArgumentNullException("model");

            DestinationRoot = model.DestinationRoot;
            ImageSize = model.ImageSize;
            ShouldAddBorders = model.ShouldAddBorders;
            ShouldShortenFileNames = model.ShouldShortenFileNames;

            MaximumSizeOptions = new[]
                                     {
                                         new ImageSize("Smallest", 320, 240),
                                         new ImageSize("Smaller", 640, 480),
                                         new ImageSize("Small", 800, 600),
                                         new ImageSize("Medium", 1024, 768),
                                         new ImageSize("Large", 1280, 1024),
                                         new ImageSize("Original size", 0, 0),
                                     };

            _view = view;
            _model = model;
        }

        public ImageSize[] MaximumSizeOptions { get; private set; }

        private string _destinationRoot;
        public string DestinationRoot
        {
            get { return _destinationRoot; }
            set
            {
                if (_destinationRoot == value)
                    return;
                _destinationRoot = value;
                OnPropertyChanged("DestinationRoot");
                UpdateView();
            }
        }

        private void UpdateView()
        {
            if (_view == null)
                return;

            _view.OkButtonEnabled = true;
            _view.ClearDestinationError();

            if (DestinationRoot == string.Empty)
            {
                const string errorMessage = "Enter a destination directory.";
                _view.SetDestinationError(errorMessage);
                _view.OkButtonEnabled = false;
            }
            else if (!Directory.Exists(DestinationRoot))
            {
                // TODO: This allows 'W:'; should require fully-qualified directory names.
                string errorMessage = string.Format("Could not find directory named '{0}'", DestinationRoot);

                _view.SetDestinationError(errorMessage);
                _view.OkButtonEnabled = false;
            }
            else
            {
                _view.ClearDestinationError();
                _view.OkButtonEnabled = true;
            }

            _view.SetBorderPreview(ShouldAddBorders ? BorderPreviewMode.WithBorders : BorderPreviewMode.Cropped);
        }

        public ImageSize ImageSize { get; set; }
        
        private bool _shouldAddBorders;
        private bool _shouldShortenFileNames;

        public bool ShouldAddBorders
        {
            get { return _shouldAddBorders; }
            set
            {
                if (_shouldAddBorders == value)
                    return;
                _shouldAddBorders = value;
                OnPropertyChanged("ShouldAddBorders");
                UpdateView();
            }
        }

        public bool ShouldShortenFileNames
        {
            get { return _shouldShortenFileNames; }
            set
            {
                if (_shouldShortenFileNames == value)
                    return;
                _shouldShortenFileNames = value;
                OnPropertyChanged("ShouldShortenFileNames");
                UpdateView();
            }
        }

        public bool BrowseForFolder(IFolderBrowserFactory folderBrowserFactory)
        {
            folderBrowserFactory.SelectedPath = DestinationRoot;
            folderBrowserFactory.Description = Resources.ConfigurationSettingsForm_FolderBrowserDialog_Description;

            if (folderBrowserFactory.ShowDialog())
            {
                DestinationRoot = folderBrowserFactory.SelectedPath;
                UpdateView();
                return true;
            }

            return false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public void OnLoad(object sender, EventArgs e)
        {
            string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            _view.SetVersionLabel(string.Format("Version {0}", version));

            UpdateView();
        }

        public void OnClickOkButton(object sender, EventArgs e)
        {
            _model.DestinationRoot = DestinationRoot;
            _model.ImageSize = ImageSize;
            _model.ShouldAddBorders = ShouldAddBorders;
            _model.ShouldShortenFileNames = ShouldShortenFileNames;
        }
    }
}